import axios from "axios";
import { ElNotification, ElLoading } from "element-plus";

const service = axios.create({
  baseURL: "http://118.178.238.19:3001",
  timeout: 40000,
});


let loading

// 请求拦截器
service.interceptors.request.use((config) => {
  if (sessionStorage.getItem("token")) {
    config.headers.token = sessionStorage.getItem("token");
  }


 loading = ElLoading.service({
    lock: true,
    text: 'Loading',
  })

 

  return config;
});

// 响应拦截器
service.interceptors.response.use(
  (res) => {
    loading.close()
    return res;
  },
  (err) => {
    if (err.response.status == 401) {
      ("登录过期，请重新登录");
      ElNotification.error({ message: "登录过期，请重新登录" });
    } else if (err.response.status == 404) {
      ElNotification.error({ message: "访问路径有误" });
    } else if (err.response.status == 500) {
      ElNotification.error({ message: "服务器内部错误" });
    } else if (err.response.status == 503) {
      ElNotification.error({ message: "服务器不可用" });
    }
    return Promise.reject(err);
  }
);

export default service;
